import { toAsync } from '@/common/awx'
import store from '@/store/index'
import * as IndexTypes from '@/store/mutation-types'

// 检测是否有权限，总开关打开且传递的模板id有一个有权限视为有权限
export async function fetchSubscribePermission (templateIds, isFetch = false) {
  try {
    const res = await fetchSetting(isFetch)
    return checkSubscribePermission(res, templateIds)
  } catch (e) {
    console.log(e)
    return true
  }
}

export function checkSubscribePermission (res, templateIds) {
  try {
    const { subscriptionsSetting } = res
    if (subscriptionsSetting.mainSwitch) {
      if (subscriptionsSetting.itemSettings) {
        const itemSettings = subscriptionsSetting.itemSettings
        let isAccept = false
        for (const templateId of templateIds) {
          if (!itemSettings[`${templateId}`]) {
            isAccept = true
          }
          if (itemSettings[`${templateId}`] === 'accept') {
            // 有一个权限为接受，视为接受
            isAccept = true
            break
          }
        }
        if (isAccept) {
          return true
        } else {
          return false
        }
      } else {
        return true
      }
    }
    return false
  } catch (e) {
    console.log('checkSubscribePermission', e)
    return false
  }
}

// 检查是否勾选了总是允许
/* eslint-disable */
export async function fetchSubscribeAlwaysAccept (templateIds, isFetch = false) {
  try {
    const res = await fetchSetting(isFetch)
    return checkSubscribeAlwaysAccept(res, templateIds)
  } catch (e) {
    throw e
  }
}
/* eslint-disable */
export function checkSubscribeAlwaysAccept (res, templateIds) {
  try {
    const { subscriptionsSetting } = res
    if (subscriptionsSetting.mainSwitch) {
      if (subscriptionsSetting.itemSettings) {
        const itemSettings = subscriptionsSetting.itemSettings
        let isNotAlways = false
        for (const templateId of templateIds) {
          if (itemSettings[`${templateId}`] !== 'accept') {
            isNotAlways = true
          }
        }
        if (isNotAlways) {
          return false
        } else {
          return true
        }
      } else {
        return false
      }
    }
    return false
  } catch (e) {
    console.log('checkSubscribeAlways', e)
    return false
  }
}

export async function fetchSetting (isFetch = false) {
  console.log('fetchSetting')
  if (store.state.wxSetting && !isFetch) {
    console.log('fetchSettingUsed')
    return store.state.wxSetting
  }
  console.log('fetchSettinginging')
  try {
    if (store.state.wxSettingFetchTimes > store.state.maxWxSettingFetchTimes) {
      throw new Error('fetchSettingTimesOver')
    }
    const res = await toAsync('getSetting').getSetting({
      withSubscriptions: true
    })
    if (!isFetch) {
      console.log('<---[Setting]--->', res)
    }
    console.log('fetchSettingSuccess')
    store.commit(IndexTypes.SET_WX_SETTING_FETCH_TIMES, store.state.wxSettingFetchTimes + 1)
    store.commit(IndexTypes.SET_WX_SETTING, res)
    return res
  } catch (e) {
    console.log('fetchSettingError', e)
    throw e
  }
}
